const app = getApp()

Page({
  data: {
    id: 0,
    index:0,
    item: [],
  },
  //页面载入
  onLoad: function (options) {
    app.getParam(options, (rel) => {
      this.setData({
        id: rel.id,
      })
      this.getCoupon(rel.id);
    });
  },
  //获取优惠券
  getCoupon: function (id) {
    app.api().Get('api/v1/ais/couponuser/read',{id:id},(result) => {
      if (result.code == 200) {
        this.setData({
          item: result.data,
        });
      }
    })
  },
  //分享按钮
  onShareAppMessage: function () {
    var coupon = this.data.item;
    return {
      title: '我领取了【' + coupon.name + '】优惠券,快来抢!',
      path: '/packageIndex/pages/coupon/views?ucode='+app.globalData.loginuser.ucode+'&id='+coupon.coupon_id
    }
  },
  //地图定位
  maps: function () {
    var store = this.data.item.store;
    var maps = app.util().baidutotencent(store.longitude,store.latitude)
    if (app.util().isNull(maps.latitude) || app.util().isNull(maps.longitude)) {
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          longitude = res.longitude, latitude = res.latitude;
          wx.openLocation({
            name: store.name,
            longitude: parseFloat(maps.longitude),
            latitude: parseFloat(maps.latitude),
            scale: 18,
            address: store.address
          });
        }
      })
    } else {
      wx.openLocation({
        name: store.name,
        longitude: parseFloat( maps.longitude),
        latitude: parseFloat( maps.latitude),
        address: store.address
      });
    }
  },
  //拨打电话
  callPhone: function (e) {
    app.callPhone(e);
  },
  //查看优惠券规则
  reCoupon:function(e){
    wx.navigateTo({
      url:e.currentTarget.dataset.url,
      fail: (res) => {
        app.wxAlert('未找到页面')
      }
    })
  }
});